$(function(){
    console.log('panel.js');
    // console.log($);
    // t('{"account":{"id":"6130037","type":50},"pageContext":"","countReg":true}');
    // console.log( chrome.devtools.network.request);




    listenToMsg("whatTodo", ({msg: o, replyFunc: e}) => {
        console.log(o);
        e();
        switch (o.todo) {
            case 'progress':
                console.log(o);
                break;
            case 'downUrl':
                sd(o.id, o.url);
                break;
            case 'noDownUrl':
                setTimeout(function () {
                    goUrl()
                },1000)
                break;
            case 'go':
                window.location.href = o.url;
                break;
            case 'rerush':
                window.location.reload(true);
                break;
        }

        // console.log(o);
        // console.log(e);
    })

    let noDownUrls, noDownUrlsIndex = 0, tabid;

    const
        //通知刷新页面
        r = () =>{
            sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'rerush'}).then((e) => {
                console.log(e);
            })
        },
        go = (url) => {
            sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'go', url : url}).then((e) => {
                console.log(e);
            })
        },
        noDownUrl = (fn) =>{
            $.ajax({
                type: "POST",
                url: 'http://d.yoo.com/Index/noDownUrl',
                // xhrFields: {
                //     withCredentials: true // 这里设置了withCredentials
                // },
                // header : header,
                // data: {tp:'tv'},
                dataType: 'JSON',
                success: function (data) {
                    // console.log(data.data.pageContext);
                    console.log('uid', data);
                    if(data.code == 0){
                        fn(data);
                    }else{
                        setTimeout(function () {
                            console.log('timeout')
                            noDownUrl(fn);
                        },10*1000)
                        console.log('全部更新完成');
                    }
                }
            });
        },
        goUrl = () =>{
            // console.log(tabid);
            // console.log(noDownUrls);
            if(noDownUrlsIndex >= noDownUrls.data.length){
                noDownUrlsIndex = 0;
                noDownUrl(data => {
                    if(data.code == 1){
                        console.log('没有了');
                        return;
                    }
                    noDownUrls = data;
                    goUrl();
                });
            }else{
                const urlInfo = noDownUrls.data[noDownUrlsIndex];
                noDownUrlsIndex +=1;
                chrome.tabs.update(tabid.id, {
                    'url': urlInfo.shareUrl,
                    'selected': true
                },e=>{
                    // console.log(e)
                    setTimeout(function () {
                        sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'downUrl', url :urlInfo.shareUrl, id : urlInfo.id}).then((e) => {
                            console.log(e);
                        })
                    }, 1000)

                });
            }
        },
        //更新下载链接
        sd = (id,downUrl) => {
            // console.log($('video').attr('src'));
            //
            // const downUrl = $('video').attr('src');

            $.ajax({
                type: "POST",
                url: 'http://d.yoo.com/Save/DownUrl',
                // xhrFields: {
                //     withCredentials: true // 这里设置了withCredentials
                // },
                // header : header,
                data: {url : downUrl, id : id},
                dataType: 'JSON',
                success: function (data) {
                    // console.log(data.data.pageContext);
                    console.log('uid', data);
                    setTimeout(function () {
                        goUrl()
                    },1000)
                }
            });

        }
    ;

    $('#sendMsg').bind('click', function () {
        // console.log(chrome.devtools.inspectedWindow);

//         sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'test'}).then((e) => {
//             console.log(e);
//         })
// return;
        /**
         * 1、获取要更新下载链接的列表
         * 2、提取第一条，更新被访问页面
         * 3、给被访问页面发送消息，提取downurl并更新到数据库
         * 4、发送消息给后端
         * 5、后端接到消息提取第二条，更新被访问页面
         * 6、进行第三步
         */

        getCurrentTab(tab =>{
            tabid = tab;
        });

        noDownUrl(data => {
            if(data.code == 1){
                console.log('没有了');
                return;
            }
            noDownUrls = data;
            goUrl(tabid);
        });



        // getCurrentTab(tab =>{
        //     chrome.tabs.update(tab.id, {
        //         'url': 'https://www.baidu.com',
        //         'selected': true
        //     },e=>{
        //        // console.log(e)
        //
        //         sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'downUrl', url :''}).then((e) => {
        //             console.log(e);
        //         })
        //     });
        // });





    });

    $('#list').bind('click', function () {
        sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'list'}).then((e) => {
            console.log(e);
        })


    });

    $('#play').bind('click', function () {

        sendMsgToTab(chrome.devtools.inspectedWindow.tabId, "whatTodo", {todo : 'play'}).then((e) => {
            console.log(e);
        })


    });

    /**
     * 从network里监听请求
     */
    // function nwk(){
    //     if ("yoo.qq.com" === location.hostname) {
            chrome.devtools.network.onRequestFinished.addListener(
                function (request) {
                    if (request.request.url.indexOf('user_work_list') > 0) {
                        console.log(request);
                        const url = request.request.url;
                        // const cookie = request.request.cookies;
                        // const header = request.request.headers;
                        if (url == '' || url == undefined) {
                            console.log('none');
                            return;
                        }
                        // const data = {id: id, url: url};
                        // $.ajax({
                        //     type: "POST",
                        //     url: 'http://d.yoo.com/Save/Downurl',
                        //     // xhrFields: {
                        //     //     withCredentials: true // 这里设置了withCredentials
                        //     // },
                        //     // header : header,
                        //     data: data,
                        //     success: function (data) {
                        //         console.log(data);
                        //     }
                        // });
                    }


                }
            );
    //     }
    // }



});